package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.query.album.TrackMediaInfoVo;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class VodServiceImpl implements VodService {
    @Autowired
    private VodConstantProperties vodConstantProperties;
    /**
     * 注册认证组件
     */
    @Autowired
    private Credential credential;

    /**
     * 获取云服务音频文件信息
     *
     * @param mediaFileId 媒体文件唯一标识
     * @return trackMediaInfoVo
     */
    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        try {
            //1 创建云应用客户端: 注册认证组件,区域属性
            VodClient vodClient = new VodClient(credential, vodConstantProperties.getRegion());
            //2.实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);
            //3.返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = vodClient.DescribeMediaInfos(req);
            //4.解析结果，获取基本信息中类型以及元数据时长跟大小
            if (resp != null) {
                MediaInfo mediaInfo = resp.getMediaInfoSet()[0];
                if (mediaInfo != null) {
                    //4.1 获取基本信息
                    MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
                    //4.2 获取元数据
                    MediaMetaData metaData = mediaInfo.getMetaData();
                    //4.3 封装音频文件详情VO
                    TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
                    trackMediaInfoVo.setType(basicInfo.getType());
                    trackMediaInfoVo.setSize(metaData.getSize());
                    trackMediaInfoVo.setDuration(metaData.getDuration());
                    return trackMediaInfoVo;
                }
            }
            return null;
        } catch (TencentCloudSDKException e) {
            log.error("获取音视频文件信息失败", mediaFileId, e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除云应用音频文件
     * @param mediaFileId 文件标识ID
     * */
    @Override
    public void deleteMedia(String mediaFileId) {
        try {
            //1.实例化一个认证对象
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            //2.实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            //3.实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(mediaFileId);
            //4.返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            log.error("[专辑服务]删除点播平台文件异常：",e);
//            此处不抛出异常,删除服务不需打断正常业务流程
//            throw new RuntimeException(e);
        }
    }
}
